package de.mhu.mandalaui.properties;

import de.mhu.mandalaui.Component;
import de.mhu.mandalaui.Response;
import de.mhu.mandalaui.StyleFilter;
import de.mhu.mandalaui.StyleFilter.FILTER;
import de.mhu.mandalaui.attributes.AppendBooleanAttribute;

public abstract class Property extends Component {

	@Override
	public void render(Response response) {

		boolean editable = false;
		PropertiesForm form = getPropertiesForm();
		if (form != null) {
			editable = form.isEditable();
		}
		Component view = getView();
		Component edit = getEdit();
		if (editable && edit != null) {
			StyleFilter viewFilter = new StyleFilter(this);
			viewFilter.setDisabled(new FILTER[] {FILTER.VISIBLE});
			viewFilter.setMyBindVisible(new AppendBooleanAttribute( "!(",form.getBindEditMode(),")") );
			StyleFilter editFilter = new StyleFilter(this);
			editFilter.setDisabled(new FILTER[] {FILTER.VISIBLE});
			editFilter.setMyBindVisible(form.getBindEditMode());

			response.startTag("div", null, viewFilter);
			if (view != null) {
				response.containerEnter(view);
				view.render(response);
				response.containerLeave();
			}
			response.endTag("div");
			response.startTag("div", null, editFilter, "display", "none");
			if (edit != null) {
				response.containerEnter(edit);
				edit.render(response);
				response.containerLeave();
			}
			response.endTag("div");
			
		} else {
			if (view != null) {
				response.containerEnter(view);
				view.render(response);
				response.containerLeave();
			}
		}
	}

	public abstract Component getView();
	
	public abstract Component getEdit();

	public PropertiesForm getPropertiesForm() {
		Component p = getParent();
		if (p != null && p instanceof PropertiesForm) {
			return (PropertiesForm)p;
		}
		return null;
	}
}
